package com.yhk.wuchat.common.common.event.listener;

import com.yhk.wuchat.common.common.event.UserBlackEvent;
import com.yhk.wuchat.common.user.dao.UserDao;
import com.yhk.wuchat.common.user.domain.entity.User;
import com.yhk.wuchat.common.user.service.cache.UserCache;
import com.yhk.wuchat.common.websocket.service.WebSocketService;
import com.yhk.wuchat.common.websocket.service.adapter.WebSocketAdapter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.nio.channels.Channel;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/4/17 21:54
 * @注释
 */
@Component
public class UserBlackListener {
    @Resource
    private WebSocketService webSocketService;
    @Resource
    private UserDao userDao;
    @Resource
    private UserCache userCache;

    @Async
    @TransactionalEventListener(classes = UserBlackEvent.class,fallbackExecution = true)
    public void sendMsg(UserBlackEvent event){
        User user = event.getUser();
        webSocketService.sendMsgToAll(WebSocketAdapter.buildBlack(user));
    }
    @Async
    @TransactionalEventListener(classes = UserBlackEvent.class,fallbackExecution = true)
    public void changeUserStatus(UserBlackEvent event){
        userDao.invalidUid(event.getUser().getId());
    }

    @Async
    @TransactionalEventListener(classes = UserBlackEvent.class,fallbackExecution = true)
    public void evictCache(UserBlackEvent event){
        userCache.evictBlackMap();
    }
}
